package model;

import java.util.Date;

/**
 * <p>
 * Represent the basic result of the execution of a solver or preprocessor.
 * </p>
 * <p>
 * It contains a description, the standard and error output,when it begin, finish, the exit value
 * and the amount of memory used.
 * </p>
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class BasicResult {
	public static final String	DEFAULT_DESCRIPTION	= "Result";
	public static final String	DEFAULT_STDERR		= "Nothing";
	public static final String	DEFAULT_STDIN		= "Nothing";
	public static final String	DEFAULT_UNKNOW		= "Unknow";

	protected Date				begin				= new Date();
	protected Date				end					= new Date(Long.MAX_VALUE);
	// protected int timeout = -1; //

	protected String			memoryUsed			= DEFAULT_UNKNOW;
	protected int				exitVal				= 0;
	protected String			stderr				= DEFAULT_STDERR;
	protected String			stdin				= DEFAULT_STDERR;
	protected String			description			= DEFAULT_DESCRIPTION;

	/**
	 * @return the memory used
	 */
	public String getMemoryUsed() {
		return memoryUsed;
	}

	/**
	 * change the memory used
	 * 
	 * @param memoryUsed
	 *            new value
	 */
	public void setMemoryUsed(String memoryUsed) {
		this.memoryUsed = memoryUsed;
	}

	/**
	 * @return the exit value of the program
	 */
	public int getExitVal() {
		return exitVal;
	}

	/**
	 * change the exit value of the program
	 * 
	 * @param exitVal
	 *            new value
	 */
	public void setExitVal(int exitVal) {
		this.exitVal = exitVal;
	}

	/*
	 * public int getTimeout() { return timeout; }
	 * 
	 * public void setTimeout(int timeout) { this.timeout = timeout; }
	 */

	/**
	 * 
	 * @return the error output.
	 */
	public String getStderr() {
		return stderr;
	}

	/**
	 * Change the error output.
	 * 
	 * @param stderr
	 *            new value.
	 */
	public void setStderr(String stderr) {
		this.stderr = stderr;
	}

	/**
	 * Add the parameter to the current stderr.
	 * 
	 * @param stderr
	 *            the string to add.
	 */
	public void addStderr(String stderr) {
		if (this.stderr.equals(DEFAULT_STDERR))
			this.stderr = stderr;
		else
			this.stderr += stderr;
	}

	/**
	 * 
	 * @return the standard output.
	 */
	public String getStdin() {
		return stdin;
	}

	/**
	 * Change the standard output.
	 * 
	 * @param stdin
	 *            new value.
	 */
	public void setStdin(String stdin) {
		this.stdin = stdin;
	}


	/**
	 * Add the parameter to the current standard.
	 * 
	 * @param stdin
	 *            the string to add.
	 */
	public void addStdin(String stdin) {
		if (this.stdin.equals(DEFAULT_STDIN))
			this.stdin = stdin;
		this.stdin += stdin;
	}

	/**
	 * 
	 * @return when the execution begin.
	 */
	public Date getBegin() {
		return begin;
	}

	/**
	 * Set when the execution begin.
	 * 
	 * @param begin
	 *            new value.
	 */
	public void setBegin(long begin) {
		if (begin <= end.getTime() && begin >= 0)
			this.begin.setTime(begin);
		else
			throw new IllegalArgumentException();
	}

	/**
	 * 
	 * @return when the execution end.
	 */
	public Date getEnd() {
		return end;
	}

	/**
	 * Set when the execution end.
	 * 
	 * @param end
	 *            new value.
	 */
	public void setEnd(long end) {
		if (end >= begin.getTime())
			this.end.setTime(end);
		else
			throw new IllegalArgumentException();
	}

	/**
	 * 
	 * @return how much time the program was runnig.
	 */
	public long getPreprocTime() {
		return end.getTime() - begin.getTime();
	}

	/**
	 * 
	 * @return a description of the result.
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Change the description.
	 * 
	 * @param description
	 *            new description.
	 */
	public void setDescription(String description) {
		this.description = description;
	}

}
